<?php

class BlockModel extends CI_Model{
    
    public function getBlocksByUserAndCourse ($userId, $courseId) {
        /*
        SELECT groups.blockId, blocks.name
        FROM user_group, groups, blocks
        WHERE user_group.userId = 4
        AND groups.courseId = 5
        AND user_group.groupId = groups.groupId
        AND blocks.blockId = groups.blockId
        GROUP BY blocks.name
        ORDER BY blocks.name
        */
        
        $this->db->select ('groups.blockId, blocks.name');
        $this->db->from ('user_group, groups, blocks');
        $this->db->where('user_group.userId',$userId);
        $this->db->where('groups.courseId',$courseId);
        $this->db->where('user_group.groupId = groups.groupId'); 
        $this->db->where('blocks.blockId = groups.blockId');
        $this->db->group_by('blocks.name');
        $this->db->order_by('blocks.name');
        $query = $this->db->get();
        return $query->result();
        
    }
    
    public function getGroupId($courseId, $blockId){
       
        $this->db->select ('groupId');
        $this->db->from ('groups');
        $this->db->where('courseId',$courseId);
        $this->db->where('blockId',$blockId);
        $query = $this->db->get();
        $group = $query->result();
        return $group[0]->groupId;
    }
    
    public function updateBlockState($groupId, $state){
        $dataToUpdate = array(
               'state' => $state
            );
        $this->db->where('groupId', $groupId);
        $this->db->update('groups', $dataToUpdate);
    }
    
    public function getGroupByState($userId, $state){
        
        /*
        SELECT user_group.groupId, courses.name, blocks.name
        FROM user_group, groups, blocks, courses
        WHERE userId =4
        AND user_group.groupId = groups.groupId
        AND groups.courseId = courses.courseId
        AND groups.blockId = blocks.blockId
        AND groups.state =  'habilitado'
        */
        
        $this->db->select ('user_group.groupId, courses.name as courseName, blocks.name as blockName');
        $this->db->from ('user_group, groups, blocks, courses');
        $this->db->where('userId',$userId);
        $this->db->where('user_group.groupId = groups.groupId');
        $this->db->where('groups.courseId = courses.courseId');
        $this->db->where('groups.blockId = blocks.blockId');
        $this->db->where('groups.state', $state);
        
        $query = $this->db->get();
        $group = $query->result();
        return $group;
      
    }
    
}
